﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace Bricks.BaseFramework.GenericControls
{
    /// <summary>
    /// TitleBar.xaml 的交互逻辑
    /// </summary>
    public partial class TitleBar : UserControl
    {
        Window topWindow;  //最顶级父窗体

        #region 枚举和属性
        /// <summary>定义控件样式的枚举
        /// </summary>
        public enum ControlStyles
        {
            /// <summary>
            /// 显示所有
            /// </summary>
            All,
            /// <summary>
            /// 全不显示
            /// </summary>
            None,
            /// <summary>
            /// 显示关闭
            /// </summary>
            Close,
            /// <summary>
            /// 显示最小化和关闭
            /// </summary>
            MinAndClose
            
        }

        /// <summary>控件样式属性
        /// </summary>
        public ControlStyles ControlStyle { get; set; }

        /// <summary>窗体图标
        /// </summary>
        public string WindowIcon { get; set; }

        /// <summary>窗体标题名称属性
        /// </summary>
        public string Title { get; set; }

        /// <summary>最顶级父窗体属性
        /// </summary>
        private Window TopWindow
        {
            get
            {
                if (topWindow == null)
                    topWindow = GetTopWindow(this);
                return topWindow;
            }
        }

        #endregion

        #region 初始化
        public TitleBar()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SetControlStyle();  //设置控件样式

            if (TopWindow == null)
                return;
            if (TopWindow.WindowStyle != System.Windows.WindowStyle.None)
            {
                TopWindow.WindowStyle = System.Windows.WindowStyle.None;
            }
            InitializeImageAndTitle();
            this.MouseDoubleClick += new MouseButtonEventHandler(TitleBar_MouseDoubleClick);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(TitleBar_MouseLeftButtonDown);
        }

        /// <summary>设置ControlStyle
        /// </summary>
        private void SetControlStyle()
        {
            if (TopWindow == null)
                return;
            //若主窗体启动时即最大化，那么最大化按钮合拢，还原按钮显示
            if (TopWindow.WindowState == WindowState.Maximized)
            {
                btnMax.Visibility = Visibility.Collapsed;
                btnRestore.Visibility = Visibility.Visible;
            }

            if (ControlStyle.Equals(ControlStyles.None))
            {
                btnMin.Visibility = btnMax.Visibility = btnClose.Visibility = Visibility.Collapsed;
            }
            else if (ControlStyle.Equals(ControlStyles.Close))
            {
                btnMin.Visibility = btnMax.Visibility = Visibility.Collapsed;
            }
            else if (ControlStyle.Equals(ControlStyles.MinAndClose))
            {
                btnMax.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>初始化图片路径及标题文字
        /// </summary>
        private void InitializeImageAndTitle()
        {
            if (!String.IsNullOrWhiteSpace(WindowIcon))
            {
                BitmapImage biTitle = new BitmapImage();
                biTitle.BeginInit();
                biTitle.UriSource = new Uri(WindowIcon, UriKind.Relative);
                biTitle.EndInit();
                this.imgLeftTop.Stretch = Stretch.Fill;
                this.imgLeftTop.Source = biTitle;
            }
            if (Title != string.Empty)
            {
                LblTiltleContent.Content = Title;
            }
        }
        #endregion

        #region 递归获取父窗体

        private Window GetTopWindow(FrameworkElement element)
        {
            if (element == null)
                return null;
            if (element is Window)
                return element as Window;
            if (element.Parent != null)
                return GetTopWindow(element.Parent as FrameworkElement);
            else
                return null;
        }
        #endregion

        #region 窗体操作
        /// <summary>窗体最大化/还原
        /// </summary>
        private void WindowMaxAndRestore()
        {
            if (TopWindow == null)
            {
                return;
            }
            TopWindow.MaxHeight = System.Windows.SystemParameters.MaximizedPrimaryScreenHeight - 8;  //将窗体的最大高度设置为主显示器上最大化顶级窗口的高度，此目的是防止最大化=全屏的缺陷
            TopWindow.MaxWidth = System.Windows.SystemParameters.MaximizedPrimaryScreenWidth - 8;

            //此项可防止最大化时为全屏
            //TopWindow.MaxHeight = SystemParameters.WorkArea.Height;   //将窗体最大高度设置为工作区域的高度，此代码也可行
            //TopWindow.MaxWidth = SystemParameters.WorkArea.Width;

            if (TopWindow.WindowState == System.Windows.WindowState.Normal)
            {
                TopWindow.WindowState = System.Windows.WindowState.Maximized;
                //最大化完成后，ImgMax隐藏，ImgRestore显示               
                btnMax.Visibility = Visibility.Collapsed;
                btnRestore.Visibility = Visibility.Visible;
            }
            else
            {
                TopWindow.WindowState = System.Windows.WindowState.Normal;
                //还原窗体大小完成后，ImgMax显示，ImgRestore隐藏
                btnMax.Visibility = Visibility.Visible;
                btnRestore.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 窗体最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            if (TopWindow == null)
            {
                return;
            }
            TopWindow.WindowState = System.Windows.WindowState.Minimized;
        }

        /// <summary>
        /// 窗体最大化及还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMax_Click(object sender, RoutedEventArgs e)
        {
            WindowMaxAndRestore();
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (TopWindow == null)
            {
                return;
            }
            TopWindow.Close();
        }

        /// <summary>
        /// 双击窗体：双击标题栏的时候才改变窗体的大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TitleBar_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (ControlStyle.Equals(ControlStyles.All))  //只有显示了最大化按钮，才可以执行此操作
            {
                WindowMaxAndRestore();
            }
        }

        /// <summary>
        /// 移动窗体：只有点击标题栏方可进行移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (TopWindow == null)
            {
                return;
            }
            try
            {
                TopWindow.DragMove();
            }
            catch
            {
            }
            //Point p = e.GetPosition(this);
            //Double width = this.ActualWidth - (btnMin.ActualWidth + btnMax.ActualWidth + btnClose.ActualWidth);  //按钮以图片方式显示时启用：除去移动窗体的MouseLeftButtonDown对按钮的作用，此目的是防止移动窗体的MouseLeftButtonDown优先于按钮的MouseLeftButtonUp，造成按钮无效
            //if (e.ChangedButton == MouseButton.Left && p.X < width)
            //{
            //TopWindow.DragMove();
            //}
        }
        #endregion
    }
}
